/**
 * @Description
 * @Author LZL
 * @Date 2025/2/25
 */


package com.kcb.coursemanager;

import com.kcb.connectDB.connectDB;
import com.kcb.choose.choose2;

import javax.swing.*;
import java.awt.*;

public class delcourse extends JFrame {
    private JTextField courseNameField;
    private JButton deleteButton, backButton;
    private String username;

    public delcourse(String username) {
        this.username = username;
        setTitle("城科课程表管理系统:课程表删除功能");
        setSize(450, 250);
        setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        setLocationRelativeTo(null);

        ImageIcon icon = new ImageIcon("C:\\Users\\TBS\\IdeaProjects\\KCB\\image\\ccst.png");
        if (icon != null) {
            setIconImage(icon.getImage());
        } else {
            System.out.println("Failed to load icon image.");
        }


        // 主面板
        JPanel panel = new JPanel();
        panel.setBackground(new Color(240, 245, 255));
        panel.setLayout(new GridBagLayout());
        GridBagConstraints gbc = new GridBagConstraints();
        gbc.insets = new Insets(10, 10, 10, 10);
        gbc.fill = GridBagConstraints.HORIZONTAL;

        JLabel titleLabel = new JLabel("删除课程");
        titleLabel.setFont(new Font("宋体", Font.BOLD, 22));
        titleLabel.setForeground(new Color(180, 50, 50));

        courseNameField = createStyledTextField();
        deleteButton = createStyledButton("删除课程", new Color(200, 50, 50));
        backButton = createStyledButton("返回", new Color(120, 120, 120));

        // 布局
        gbc.gridx = 0;
        gbc.gridy = 0;
        gbc.gridwidth = 2;
        gbc.anchor = GridBagConstraints.CENTER;
        panel.add(titleLabel, gbc);

        gbc.gridwidth = 1;
        gbc.anchor = GridBagConstraints.WEST;

        addLabel(panel, gbc, "课程名称:", 1);
        addTextField(panel, gbc, courseNameField, 1);

        gbc.gridy = 2;
        gbc.gridx = 0;
        panel.add(backButton, gbc);

        gbc.gridx = 1;
        panel.add(deleteButton, gbc);

        getContentPane().add(panel);

        // 按钮监听
        deleteButton.addActionListener(e -> deleteCourse());
        backButton.addActionListener(e -> {
            new choose2(username).setVisible(true);
            dispose();
        });
    }

    private void addLabel(JPanel panel, GridBagConstraints gbc, String text, int y) {
        gbc.gridx = 0;
        gbc.gridy = y;
        panel.add(new JLabel(text), gbc);
    }

    private void addTextField(JPanel panel, GridBagConstraints gbc, JTextField textField, int y) {
        gbc.gridx = 1;
        gbc.gridy = y;
        panel.add(textField, gbc);
    }

    private JTextField createStyledTextField() {
        JTextField textField = new JTextField(15);
        textField.setFont(new Font("宋体", Font.PLAIN, 16));
        textField.setPreferredSize(new Dimension(180, 30));
        return textField;
    }

    private JButton createStyledButton(String text, Color bgColor) {
        JButton button = new JButton(text);
        button.setFont(new Font("宋体", Font.BOLD, 16));
        button.setBackground(bgColor);
        button.setForeground(Color.WHITE);
        button.setFocusPainted(false);
        button.setBorderPainted(false);
        button.setPreferredSize(new Dimension(180, 45));
        return button;
    }

    private void deleteCourse() {
        String courseName = courseNameField.getText().trim();

        if (courseName.isEmpty()) {
            JOptionPane.showMessageDialog(null, "请输入课程名称！", "错误", JOptionPane.ERROR_MESSAGE);
            return;
        }

        connectDB dbConnector = new connectDB();
        if (!dbConnector.courseExists(username, courseName)) {
            JOptionPane.showMessageDialog(null, "该课程不存在！", "错误", JOptionPane.ERROR_MESSAGE);
            return;
        }

        int confirm = JOptionPane.showConfirmDialog(
                null, "确定要删除课程 [" + courseName + "] 吗？", "确认删除", JOptionPane.YES_NO_OPTION);
        if (confirm == JOptionPane.YES_OPTION) {
            if (dbConnector.deleteCourse(username, courseName)) {
                JOptionPane.showMessageDialog(null, "课程删除成功！");
                new choose2(username).setVisible(true);
                dispose();
            } else {
                JOptionPane.showMessageDialog(null, "删除课程失败。");
            }
        }
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(() -> new delcourse("exampleUser").setVisible(true));
    }
}
